import { _decorator, Component, game, Label, Node, Sprite, SpriteFrame } from 'cc';
import { MyButton } from './commonComp/MyButton';
import { UserSignInData } from './commonComp/InterfaceCom';
import { HttpCom } from './net/HttpCom';
import { GameEvent, PropUpType } from './EventName';
const { ccclass, property } = _decorator;

@ccclass('SignInItem')
export class SignInItem extends Component {
    @property([SpriteFrame])
    itemSf: SpriteFrame[] = []
    
    @property(Sprite)
    icon: Sprite = null;
    @property(Sprite)
    bg: Sprite = null;
    @property(Label)
    countLab: Label = null;
    @property(Label)
    dayLab: Label = null;
    @property(Node)
    isSingedNode: Node = null;

    @property(MyButton)
    signBtn: MyButton = null;

    start() {

    }

    init(){

    }

    changeSignState(data: boolean){
        
        this.isSingedNode.active = data;
        this.signBtn.interactable = !data;
        this.bg.grayscale = !data;
    }

    canSign(bo: boolean){
        
        this.signBtn.interactable = bo;
        
    }

    initView(data: any[], day: string){
    
        let type = ""
        let count = 0;
        try {

            data.forEach((e,i)=>{
                if(e.name){
    
                    type = e.name.toLowerCase();
                    count = e.value;
                    if(type == "gold"){
                        this.icon.spriteFrame = this.itemSf[0];
                    }
            
                    if(type == "ticket"){
                        this.icon.spriteFrame = this.itemSf[1];
                    }
                }
                
            })
        }
        catch(e){

        }

        this.countLab.string = count.toString();
        this.dayLab.string = "day" + "(" + day + ")";
        this.bg.grayscale = true;
        //this.signBtn.interactable = false;
    }

    sign(){
        console.log('点击签到按钮');
        HttpCom.signin((data: any)=>{
            this.isSingedNode.active = true;
            this.signBtn.interactable = false;
            //game.emit(GameEvent.SHOW_REWARD_PROPUP,data,PropUpType.SignReward)
        });
    }

    update(deltaTime: number) {
        
    }
}


